package day1.HomeWork;

import java.util.Objects;

/**
 * ClassName：Student
 *
 * @Author：小鱼
 * @Create：2025-08-18 14:37
 */
public class Student {
    private String name;
    private int age;

    //属性构造器
    public Student() {
    }

    public Student(String name, int age)throws AgeOutRangeException {
        this.name = name;
        setAge(age);
    }

    //getset方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeOutRangeException{
        //如果age的范围不在16-79，那么抛出自定义的异常AgeOutRangeException
        if (age < 16 || age > 79) {
            // 当年龄超出范围时，抛出自定义异常
            throw new AgeOutRangeException("年龄必须在16到79之间，当前年龄为：" + age);
        }
       this.age = age;
    }

    @Override
    public String toString() {
        return "\nStudent{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
